package com.rgs.se.util;

import java.io.*;

/**
 * @author Ren Gaoshuai
 * @ClassName ReadDir
 * @Description TODO
 * @date 2021-09-27 22:58
 **/
public class ReadDir {

    /**
     * 使用递归的方式调用
     */
    public void traverseFolder2(String path) throws IOException {

        //定义输出目录
        String FileOut="G:\\test.txt";
        BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(FileOut), "GB2312"));


        File file = new File(path);
        //文件存在
        if(file.exists()){
            //获取该路径下的所有文件夹
            File[] files = file.listFiles();
            if (files == null || files.length == 0){
                System.out.println("该路径的文件夹是空的");
            }else{
                //遍历该目录下的所有文件（可能是文件夹 或 文件）
                for (File file1 : files) {
                    //读取某一个文件
                    if (file1.isFile()){
                        //定义一个输入流
                        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file1),"GB2312"));
                        String line;
                        //int lineEmptyCount = 0;
                        //读一行
                        while((line=br.readLine()) != null) {
                            bw.write(line);
                            bw.newLine();
                        }
                        br.close();
                    }
                }
            }

        }else{
            System.out.println("该路径文件不存在");
        }

        System.out.println("整合完毕");
    }

    public static void main(String[] args) throws IOException {
        new ReadDir().traverseFolder2("G:\\test");
    }
}
